package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Course;
import com.qk.service.CoursesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/courses")
public class CoursesController {

    @Autowired
    private CoursesService coursesService;

    @GetMapping
    public Result pageCourses(@RequestParam(required = false) String name,
                              @RequestParam(required = false) String subject,
                              @RequestParam(required = false) String target,
                              @RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("条件分页查询课程：name={}，subject={},target={},page={},pageSize={}", name, subject, target, page, pageSize);
        PageResult<Course> pageResult = coursesService.pageCourses(name, subject, target, page, pageSize);
        return Result.success(pageResult);
    }

    @DeleteMapping("/{id}")
    public Result deleteCourse(@PathVariable Long id) {
        log.info("删除课程：{}", id);
        coursesService.removeCourseById(id);
        return Result.success();
    }

    @PostMapping
    public Result addCourse(@RequestBody Course course) {
        log.info("添加课程：{}", course);
        coursesService.saveCourse(course);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getCourseById(@PathVariable Long id) {
        log.info("查询课程：{}", id);
        Course course = coursesService.getCourseById(id);
        return Result.success(course);
    }

    @PutMapping
    public Result updateCourse(@RequestBody Course course) {
        log.info("修改课程：{}", course);
        coursesService.updateCourse(course);
        return Result.success();
    }

    @GetMapping("/list")
    public Result listAllCourses() {
        log.info("查询所有课程");
        List<Course> courses = coursesService.listAllCourses();
        return Result.success(courses);
    }

    @GetMapping("/subject/{subject}")
    public Result listCoursesBySubject(@PathVariable String subject) {
        log.info("查询课程：{}", subject);
        List<Course> courses = coursesService.listCoursesBySubject(subject);
        return Result.success(courses);
    }



}
